package com.distribution.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.distribution.common.core.domain.entity.SysUser;
import com.distribution.common.exception.ServiceException;
import com.distribution.common.utils.StringUtils;
import com.distribution.framework.config.ServerConfig;
import com.distribution.system.domain.dto.AgentDTO;
import com.distribution.system.domain.dto.CmccDaOrderListDTO;
import com.distribution.system.domain.entity.CmccDaOrder;
import com.distribution.system.mapper.CmccDaOrderMapper;
import com.distribution.system.mapper.SysUserMapper;
import com.distribution.system.service.CmccDaSubordinateOrderService;
import com.distribution.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.distribution.common.utils.SecurityUtils.getUserId;

/**
 * @author destinyhlp
 * @date 2023/6/3 22:21
 */

@Service
public class CmccDaSubordinateOrderServiceImpl extends ServiceImpl<CmccDaOrderMapper, CmccDaOrder> implements CmccDaSubordinateOrderService {
    @Value("${swagger.pathMapping}")
    private String pathMapping;
    @Resource
    ServerConfig serverConfig;
    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    ISysUserService userService;

    @Override
    public List<CmccDaOrder> listCmccDaSubordinateOrder(CmccDaOrderListDTO cmccDaOrderListReqToDto) {
        //System.out.println("cmccDaOrderListReqToDto = " + cmccDaOrderListReqToDto);
        List<Long> userIdList = cmccDaOrderListReqToDto.getUserIdList();
        if (userIdList == null || userIdList.isEmpty()) {
            throw new ServiceException("查不到下级代理！");
        }
        LambdaQueryWrapper<CmccDaOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(CmccDaOrder::getUserId, userIdList);
        if (!Objects.isNull(cmccDaOrderListReqToDto.getProductTypeId())) {
            queryWrapper.eq(CmccDaOrder::getProductTypeId, cmccDaOrderListReqToDto.getProductTypeId());
        }
        if (!StringUtils.isEmpty(cmccDaOrderListReqToDto.getChannelOrderNo())) {
            queryWrapper.eq(CmccDaOrder::getChannelOrderNo, cmccDaOrderListReqToDto.getChannelOrderNo());
        }
        if (!Objects.isNull(cmccDaOrderListReqToDto.getOrderStatus())) {
            queryWrapper.eq(CmccDaOrder::getOrderStatus, cmccDaOrderListReqToDto.getOrderStatus());
        }
        if (!StringUtils.isEmpty(cmccDaOrderListReqToDto.getChoosePhone())) {
            queryWrapper.like(CmccDaOrder::getChoosePhone, cmccDaOrderListReqToDto.getChoosePhone());
        }
        if (!StringUtils.isEmpty(cmccDaOrderListReqToDto.getUserName())) {
            queryWrapper.like(CmccDaOrder::getUserName, cmccDaOrderListReqToDto.getUserName());
        }
        if (!StringUtils.isEmpty(cmccDaOrderListReqToDto.getProductName())) {
            queryWrapper.like(CmccDaOrder::getProductName, cmccDaOrderListReqToDto.getProductName());
        }
        if (!StringUtils.isEmpty(cmccDaOrderListReqToDto.getOrderId())) {
            queryWrapper.like(CmccDaOrder::getOrderId, cmccDaOrderListReqToDto.getOrderId());
        }
        if (!StringUtils.isEmpty(cmccDaOrderListReqToDto.getCustomerName())) {
            queryWrapper.like(CmccDaOrder::getCustomerName, cmccDaOrderListReqToDto.getCustomerName());
        }
        if (!StringUtils.isEmpty(cmccDaOrderListReqToDto.getCustomerPhone())) {
            queryWrapper.like(CmccDaOrder::getCustomerPhone, cmccDaOrderListReqToDto.getCustomerPhone());
        }
        if (!Objects.isNull(cmccDaOrderListReqToDto.getEndTime())) {
            queryWrapper.le(CmccDaOrder::getCreateTime, cmccDaOrderListReqToDto.getEndTime());
        }
        if (!Objects.isNull(cmccDaOrderListReqToDto.getBeginTime())) {
            queryWrapper.ge(CmccDaOrder::getCreateTime, cmccDaOrderListReqToDto.getBeginTime());
        }
        if (!Objects.isNull(cmccDaOrderListReqToDto.getBindChannelInfoId())) {
            queryWrapper.eq(CmccDaOrder::getBindChannelInfoId, cmccDaOrderListReqToDto.getBindChannelInfoId());
        }
        queryWrapper.orderByDesc(CmccDaOrder::getCreateTime);
        List<CmccDaOrder> orderList = list(queryWrapper);
        String prefix = serverConfig.getUrl() + pathMapping;
        orderList.forEach(f -> {
            f.setProductImg(prefix + f.getProductImg());
        });
        return orderList;
    }

    @Override
    public List<Long> getSubordinateUserIdList() {
        Long userId = getUserId();
        SysUser currentSysUser = sysUserMapper.selectUserById(userId);
        List<SysUser> sysUsers;
        if (currentSysUser.isOperationSupervisor()){
            sysUsers = sysUserMapper.selectAllUserList();
        }else {
            AgentDTO agentDTO = new AgentDTO();
            agentDTO.setParentId(userId);
//            sysUsers = sysUserMapper.selectAllUserListByParentId(agentDTO);
            sysUsers = userService.selectUserListByParentId(agentDTO);
        }
        if (sysUsers == null || sysUsers.isEmpty()) {
            //throw new ServiceException("查询不到下级用户！");
            return null;
        }
        return sysUsers.stream().map(SysUser::getUserId).collect(Collectors.toList());
    }
}
